import sys,os
from datetime import datetime

def execute(command, printToScreen=False):
    if(printToScreen):
        print('command:', command)
    os.system(command)

def mkdirs(dir_names):
    for dn in dir_names:
        mkdir(dn)
        
def mkdir(dir_name):
    if(not os.path.exists(dir_name)):
        os.mkdir(dir_name)
            
def clear_dir(dir_name):
    os.rmdir(dir_name)

def delete_files(dir_path):
    for file in os.listdir(dir_path):
        file_path = os.path.join(dir_path, file)
        if os.path.isfile(file_path): 
            os.remove(file_path)    

cur_dir = os.path.dirname(__file__)
tar_dir = sys.args[1] if len(sys.argv) > 1 else cur_dir
src_dir = os.path.join(cur_dir, 'src')
bin_dir = os.path.join(cur_dir, 'bin')
out_dir = os.path.join(cur_dir, 'output')

# create binary files directory and output directory
mkdirs([bin_dir, out_dir])
#execute(f'rm {os.path.join(bin_dir, "*.class")}')
delete_files(bin_dir)
#sys.exit(0)

# -cp=classpath 指定Java程序所在目标 后面跟上Java的 包.类名
cmd = f'javac -encoding UTF-8 -cp {cur_dir} -d {bin_dir} {os.path.join(src_dir, "*.java")}'
execute(cmd)
#execute(f'ls -l {cur_dir}')
test_key = f'test_{datetime.strftime(datetime.today(), "%Y%m%d_%H%M%S")}'
test_dir = os.path.join(out_dir, test_key)
mkdir(test_dir)
for f in os.listdir(tar_dir):
    file = os.path.join(tar_dir, f)
    if os.path.isfile(file):
        execute(f'java -cp {bin_dir} AesCryption {file} {test_dir}')
        execute(f'java -cp {bin_dir} FileEncryption {file} {test_dir}')



